package com.cyufeng.blog.service;

import com.cyufeng.blog.dto.AccountDto;
import com.cyufeng.blog.dto.ArticleDto;
import com.cyufeng.blog.entity.Account;
import com.cyufeng.blog.entity.Article;
import com.cyufeng.blog.entity.Review;
import com.cyufeng.blog.repository.AccountRepository;
import com.cyufeng.blog.service.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户登陆
 *
 * @author Cyufeng
 * Data:2018.12.30
 */
@Service
public class AccountService {
    @Autowired
    AccountRepository accountRepository;

    AccountMapper loginMapper = new AccountMapper();

    /**
     * 登陆验证
     *
     * @param username
     * @param passwd
     * @return
     */
    public AccountDto getUser(String username, String passwd) {
        AccountDto loginDto = new AccountDto();

        List<Account> userList = new ArrayList<>();
        userList = accountRepository.getUser(username, passwd);
        if (userList.size() == 1) {
            loginDto = loginMapper.entityTodto(userList.get(0));
        } else {
            loginDto = null;
        }

        return loginDto;
    }

    /**
     * 得到所有的用户
     *
     * @return
     */
    public List<AccountDto> getAllAccount(int isAdmin) {
        List<AccountDto> loginDto = new ArrayList<>();

        List<Account> userList = new ArrayList<>();
        userList = accountRepository.getAllAccount(isAdmin);
        loginDto = loginMapper.entityTodto(userList);

        return loginDto;
    }

    //    修改是否启用
    @Transactional
    public String setIsEnbale(Boolean isEnable, int id) {
        int tempIsEnable = 0;
        try {
            if (isEnable == true)
                tempIsEnable = 1;
            accountRepository.setIsEnable(tempIsEnable, id);
            return "success";
        } catch (Exception e) {
            return "error";
        }
    }

    //    修改编辑内容
    @Transactional
    public String saveEdit(String passwd, int id) {
        try {
            accountRepository.saveEdit(passwd, id);
            return "success";
        } catch (Exception e) {
            return "error";
        }
    }

    //    删除用户
    @Transactional
        public String deleteUser(int id) {
            try {
                java.util.Date utilDate=new Date();
                java.sql.Date sqlDate=new java.sql.Date(utilDate.getTime());
                accountRepository.deleteUser(sqlDate,id);
                return "success";
            } catch (Exception e) {
                return "error";
            }
    }

    /**
     * 保存文章
     *
     * @param accountDto
     * @return
     */
    public String saveUser(AccountDto accountDto) {
        try {
//            将从前台传来的dto通过mapper转为entity再保存entity
            Account account = new Account();
            account = loginMapper.dtoToentity(accountDto);
            account.setMadeTime(new Date());
            accountRepository.save(account);
            return "success";
        } catch (Exception e) {
            return "error";
        }
    }
}
